﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Http;
using Jw.Core;
using Jw.Models;
using Jw.Web.App_Start;

namespace Jw.Web.Areas.Basic.Controllers
{
    public class WlzdController : BaseController
    {
        //物料字典
        // GET: /Basic/Wlzd/
        [MvcMenuFilter(false)]
        public ActionResult Index()
        {
            sys_codeService codeService = new sys_codeService();

            var wlxzItems = new List<dynamic>();
            wlxzItems.Add(new { value = "原材料", text = "原材料" });
            wlxzItems.Add(new { value = "产成品", text = "产成品" });
            wlxzItems.Add(new { value = "设备", text = "设备" });

            var unitItems = codeService.GetTextListByType("MeasureUnit");
            var model = new
            {
                dataSource = new
                {
                    //物料性质
                    wlxzItems = wlxzItems,
                    //计量单位
                    unitItems = unitItems
                }
            };
            return View(model);
        }

    }
    /// <summary>
    /// 物料字典
    /// </summary>
    public class WlzdApiController : ApiController
    {
        /// <summary>
        /// 物料类别
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public dynamic GetTypes(RequestWrapper request)
        {
            request.LoadSettingXmlString(@"
            <settings defaultOrderBy='Wllb_Lbbh'>
               <where defaultIgnoreEmpty='true'>
                    <field name='Wllb_Lbbh'      cp='equal'></field>
                    <field name='Wllb_Lbmc'  cp='like' ></field>
                </where>
            </settings>
            ");
            var result = new l_WllbService().GetDynamicList(request.ToParamQuery());
            return result;
        }

        /// <summary>
        /// 物料字典
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public dynamic Get(RequestWrapper request)
        {
            request.LoadSettingXmlString(@"
            <settings defaultOrderBy='Wlzd_Wlbh'>
               <where>
                    <field name='Wlzd_Lbbh' cp='equal' ignoreEmpty='true'></field>
                </where>
            </settings>");
            var service = new l_WlzdService();
            var result = service.GetModelListWithPaging(request.ToParamQuery());
            return result;
        }

        /// <summary>
        /// 得到物料字典新编号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetNewCode(string id)
        {
            var service = new l_WlzdService();
            return service.GetNewWlzdCode(id);
        }

        /// <summary>
        /// 物料字典编辑
        /// </summary>
        /// <param name="data"></param>
        [System.Web.Http.HttpPost]
        public void Edit(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>l_Wlzd</table>
                <where>
                    <field name='Wlzd_Wlbh' cp='equal'></field>
                </where>
            </settings>");
            var service = new l_WlzdService();
            var result = service.Edit(null, listWrapper, data);
        }

        /// <summary>
        /// 物料类别-编辑
        /// </summary>
        /// <param name="data"></param>
        [System.Web.Http.HttpPost]
        public void EditType(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
            <settings>
                <table>l_Wllb</table>
                <where>
                    <field name='Wllb_Lbbh' cp='equal'></field>
                </where>
            </settings>");
            var service = new l_WllbService();
            var result = service.Edit(null, listWrapper, data);
        }
    }
}
